package com.gxa.servlet.goods;

import com.alibaba.fastjson.JSON;
import com.gxa.pojo.Result;
import com.gxa.pojo.goods.Goods;
import com.gxa.service.goods.GoodsService;
import com.gxa.service.goods.impl.GoodsServiceimpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet(urlPatterns = "/GoodsUpdateSServlet")
public class GoodsUpdateSServlet extends HttpServlet {
    GoodsService goodsService = new GoodsServiceimpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
//        BufferedReader reader = request.getReader();
//        StringBuilder stringBuilder = new StringBuilder();
//        String sr;
//        while((sr=reader.readLine())!=null){
//            stringBuilder.append(sr);
//        }
//        String s = stringBuilder.toString();
//        Goods goods = JSON.parseObject(s, Goods.class);
        String gid = request.getParameter("gid");
        String state = request.getParameter("state");
        int i1 = Integer.parseInt(gid);
        int i2 = Integer.parseInt(state);
        int i = goodsService.updates(i1,i2);
        Map<String, Object> map = new HashMap<>();
        if (i!=0){
            map.put("Result", Result.ok("修改成功"));
        }else {
            map.put("Result",Result.error());
        }
        String s1 = JSON.toJSONString(map);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(s1);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
